package com.jiankangjin.appspos.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.appspos.services.ReportServices;
import com.jiankangjin.appspos.util.CheckToken;
import com.jiankangjin.appspos.util.StringUtil;
import com.jiankangjin.appspos.vo.GetReportResultVo;
import com.jiankangjin.appspos.vo.GetUserInfoVo;
import com.jiankangjin.appspos.vo.SearchReportVo;

@Controller
public class ReportController {
	private static final transient Log logger = LogFactory.getLog(ReportController.class);
	
	@Autowired
	private ReportServices reportService;
	
	/**
	 * 商户报表查询
	 */
	@RequestMapping(value="/search_report", method = RequestMethod.POST)
	@ResponseBody
	public SearchReportVo searchReport(
			@RequestParam(required = false) String token,
			@RequestParam(required = false) String date_begin,
			@RequestParam(required = false) String date_end,
			@RequestParam(required = false) String search_word,
			@RequestParam(required = false) String sign) {
		StringBuilder sb = new StringBuilder();
		sb.append("token=" + token);
		sb.append(" date_begin=" + date_begin);
		sb.append(" date_end=" + date_end);
		sb.append(" search_word=" + search_word);
		sb.append(" sign=" + sign);
		if (logger.isDebugEnabled()) {
			logger.info("商户报表查询接口:" + sb);
		}
		SearchReportVo searchReportVo = new SearchReportVo();
		searchReportVo.setResp_status("9999");
		if (StringUtil.isEmpty(token)) {
			searchReportVo.setMsg("token不能为空");
			return searchReportVo;
		}
		if (StringUtil.isEmpty(sign)) {
			searchReportVo.setMsg("sign不能为空");
			return searchReportVo;
		}
		// Token验证
		GetUserInfoVo userInfo = null;
		userInfo = CheckToken.getUserInfo(token);
	    if (!"0000".equals(userInfo.getResp_status())) {
	    	searchReportVo.setMsg("token 验证失败");
	    	return searchReportVo;
	    }
		return reportService.searchReport(token, date_begin, date_end, search_word);	
	}
	/**
	 * 商户报表查询结果查看
	 */
	@RequestMapping(value="/get_report_result", method = RequestMethod.POST)
	@ResponseBody
	public GetReportResultVo getReportResult(
			@RequestParam(required = false) String token,
			@RequestParam(required = false) String result_id,
			@RequestParam(required = false) String page,
			@RequestParam(required = false) String quantity,
			@RequestParam(required = false) String sign) {
		StringBuilder sb = new StringBuilder();
		sb.append("token=" + token);
		sb.append(" result_id=" + result_id);
		sb.append(" page=" + page);
		sb.append(" quantity=" + quantity);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("商户报表查询结果查看接口:" + sb);
		}
		GetReportResultVo getReportResultVo = new GetReportResultVo();
		getReportResultVo.setResp_status("9999");
		if (StringUtil.isEmpty(token)) {
			getReportResultVo.setMsg("token不能为空");
			return getReportResultVo;
		}
		if (StringUtil.isEmpty(result_id)) {
			getReportResultVo.setMsg("result_id不能为空");
			return getReportResultVo;
		}
		Integer _page = 1;
		Integer _quantity = 10;
		if (StringUtil.isNotEmpty(page)) {
			if (StringUtil.isNotNum(page)) {
				getReportResultVo.setMsg("page必须是数字");
				return getReportResultVo;
			} else {
				_page = Integer.parseInt(page);
			}
		}
		if (StringUtil.isNotEmpty(quantity)) {
			if (StringUtil.isNotNum(quantity)) {
				getReportResultVo.setMsg("quantity必须是数字");
				return getReportResultVo;
			} else {
				_quantity = Integer.parseInt(quantity);
			}
		}
		if (StringUtil.isEmpty(sign)) {
			getReportResultVo.setMsg("sign不能为空");
			return getReportResultVo;
		}
		// Token验证
		GetUserInfoVo userInfo = null;
		userInfo = CheckToken.getUserInfo(token);
		if (!"0000".equals(userInfo.getResp_status())) {
			getReportResultVo.setMsg("token 验证失败");
			return getReportResultVo;
	    }
		return reportService.getReportResult(token, result_id, _page, _quantity);
	}
}
